package com.snail.readyvr.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.snail.readyvr.R;
import com.snail.readyvr.application.ConfigurationWrapper;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.util.Utils;

import butterknife.ButterKnife;
import qiu.niorgai.StatusBarCompat;

/**
 * Created by wangyunpeng on 2016/3/10.
 * Activity基础类
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected String TAG = BaseActivity.class.getSimpleName();
    private int top_common_bar_bg_color = R.color.color_black;
    private int mAlpha = 0;
    private boolean enableSysBarTint = true;
    private Tracker mTracker;
    protected boolean isFullScreen = false;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ConfigurationWrapper.wrapLocale(newBase,Utils.getLocal(newBase)));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);//防止截屏
        if(savedInstanceState != null && !Utils.isLive){
            Intent intent = new Intent(this, WelcomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }

        if(isFullScreen){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }

        TAG = this.getClass().getSimpleName();
        super.onCreate(savedInstanceState);
        MyApplication myApplication = (MyApplication)getApplication();
        mTracker = myApplication.getDefaultTracker();
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        init();
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if(enableSysBarTint){
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(top_common_bar_bg_color), mAlpha);
        }
    }

    protected void enableSystemBarTint(boolean enable,int colorRes ,int alpha) {
        enableSysBarTint = enable;
        this.top_common_bar_bg_color = colorRes;
        this.mAlpha = alpha;
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        try {
            super.onRestoreInstanceState(savedInstanceState);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        try {
            super.onSaveInstanceState(outState);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (mTracker != null) {
            mTracker.setScreenName("Page__" + TAG);
            mTracker.send(new HitBuilders.ScreenViewBuilder().build());
        }

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    protected abstract int getLayoutId();
    protected abstract void init();
}
